/**
 * Notes: 样品管理模块控制器
 * Date: 2024-03-11
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 */

const BaseProjectController = require('./base_project_controller.js');
const SampleService = require('../service/sample_service.js');
const timeUtil = require('../../../framework/helpers/time_util.js');
const dataUtil = require('../../../framework/helpers/data_util.js');
const appCode = require('../../../framework/core/app_code.js');

class SampleController extends BaseProjectController {

    /** 获取样品列表 */
    async getSampleList() {
        // 数据校验
        let rules = {
            search: 'string|min:0|max:30|name=搜索条件',
            sortType: 'string|name=搜索类型',
            sortVal: 'name=搜索类型值',
            page: 'must|int|default=1',
            size: 'must|int',
            status: 'string|name=状态',
        };

        // 取得数据
        let input = this.validateData(rules);
        let service = new SampleService();
        let result = await service.getSampleList(input);

        // 数据格式化
        let list = result.list;
        for (let k = 0; k < list.length; k++) {
            list[k].SAMPLE_MAKE_START_TIME = timeUtil.timestamp2Time(list[k].SAMPLE_MAKE_START_TIME, 'Y-M-D');
            list[k].SAMPLE_MAKE_END_TIME = timeUtil.timestamp2Time(list[k].SAMPLE_MAKE_END_TIME, 'Y-M-D');
            list[k].SAMPLE_TO_CUSTOMER_TIME = timeUtil.timestamp2Time(list[k].SAMPLE_TO_CUSTOMER_TIME, 'Y-M-D');
            list[k].createTime = timeUtil.timestamp2Time(list[k].createTime);
        }

        return result;
    }

    /** 获取样品详情 */
    async getSampleDetail() {
        // 数据校验
        let rules = {
            id: 'must|id',
        };

        // 取得数据
        let input = this.validateData(rules);
        let service = new SampleService();
        let detail = await service.getSampleDetail(input.id);
        if (detail) {
            detail.SAMPLE_MAKE_START_TIME = timeUtil.timestamp2Time(detail.SAMPLE_MAKE_START_TIME, 'Y-M-D');
            detail.SAMPLE_MAKE_END_TIME = timeUtil.timestamp2Time(detail.SAMPLE_MAKE_END_TIME, 'Y-M-D');
            detail.SAMPLE_TO_CUSTOMER_TIME = timeUtil.timestamp2Time(detail.SAMPLE_TO_CUSTOMER_TIME, 'Y-M-D');
            detail.createTime = timeUtil.timestamp2Time(detail.createTime);
        }

        return detail;
    }

    /** 创建样品 */
    async insertSample() {
        // 数据校验
        let rules = {
            SAMPLE_CODE: 'must|string|min:2|max:50|name=存样编号',
            SAMPLE_NAME: 'must|string|min:2|max:50|name=样品名称',
            SAMPLE_VERSION: 'string|max:50|name=型号',
            SAMPLE_PROJECT_ID: 'string|name=关联项目',
            SAMPLE_STATUS: 'must|int|name=样品阶段',
            SAMPLE_LOCATION: 'string|max:100|name=位置',
            SAMPLE_MAKE_USER_ID: 'string|max:50|name=工程师',
            SAMPLE_NOTES: 'string|max:500|name=详细说明',
            SAMPLE_ATTACHMENTS: 'array|name=样品图片',
            SAMPLE_CUSTOMER: 'string|name=客户'
        };

        // 取得数据
        let input = this.validateData(rules);
        
        let service = new SampleService();
        let result = await service.insertSample(input);

        return result;
    }

    /** 更新样品 */
    async editSample() {
        // 数据校验
        let rules = {
            id: 'must|id',
            SAMPLE_CODE: 'must|string|min:2|max:50|name=存样编号',
            SAMPLE_NAME: 'must|string|min:2|max:50|name=样品名称',
            SAMPLE_VERSION: 'string|max:50|name=型号',
            SAMPLE_PROJECT_ID: 'string|name=关联项目',
            SAMPLE_STATUS: 'must|int|name=样品阶段',
            SAMPLE_LOCATION: 'string|max:100|name=位置',
            SAMPLE_MAKE_USER_ID: 'string|max:50|name=工程师',
            SAMPLE_NOTES: 'string|max:500|name=详细说明',
            SAMPLE_ATTACHMENTS: 'array|name=样品图片',
            SAMPLE_CUSTOMER: 'string|name=客户'
        };

        // 取得数据
        let input = this.validateData(rules);
        
        let service = new SampleService();
        let result = await service.editSample(input);

        return result;
    }

    /** 删除样品 */
    async delSample() {
        // 数据校验
        let rules = {
            id: 'must|id',
        };

        // 取得数据
        let input = this.validateData(rules);
        
        let service = new SampleService();
        await service.delSample(input.id);
    }

    /** 获取样品阶段列表 */
    async getSampleStatusOptions() {
        let service = new SampleService();
        return await service.getSampleStatusOptions();
    }

    /** 获取项目列表选择器 */
    async getProjectOptions() {
        let service = new SampleService();
        return await service.getProjectOptions();
    }

    /** 检查当前用户是否有权限添加样品 */
    async checkSampleAddPermission() {
        let service = new SampleService();
        return await service.checkSampleAddPermission(this._userId);
    }
}

module.exports = SampleController; 